﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using ARS.Control.Custom.DataPaging.Attributes;


namespace ARS.DataBase.Provider
{
   public  abstract class IDataPagingProvider
   {

       //public DataSet GetDataList(ARS.Control.Attributes.DbSelectAll dsa)
       //{
       //    DbSelect selectMode = (DbSelect)dsa;
       //    return GetDataList(selectMode, false);
       //}
       //public string GetDataListAllSql(ARS.Control.Attributes.DbSelectAll dsa)
       //{
       //    System.Text.StringBuilder sb = new StringBuilder();
       //    sb.Append("select ");
       //    if (string.IsNullOrEmpty(dsa.SelectFields))
       //    {
       //        sb.Append("*");
       //    }
       //    else
       //    {
       //        sb.Append(dsa.SelectFields);
       //    }
       //    sb.Append(" from ");
       //    sb.Append(dsa.TableName);
       //    if (!string.IsNullOrEmpty(dsa.StrWhere))
       //    {
       //        sb.Append(" where ");
       //        sb.Append(dsa.StrWhere);
       //    }
       //    if (!string.IsNullOrEmpty(dsa.OrderField))
       //    {
       //        sb.Append(" order by ");
       //        sb.Append(dsa.OrderField);
       //    }
       //    return sb.ToString();
       //}

       public abstract DataSet GetDataList(ARS.Control.Attributes.DbSelectAll dsa);
       /// <summary>
       /// 获取分页dataset
       /// </summary>
       /// <returns></returns>
       public abstract DataSet GetDataList(DbSelect selectMode, bool isStatistics = true);
       /// <summary>
       /// 根据主键获取Model
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public abstract DataPagingDb GetDataPagingDbModel(int id);

       public abstract DataPagingDb GetDataPagingDbModel(DataPagingQueryModel queryModel);
       /// <summary>
       /// 根据名称和值获取model
       /// </summary>
       /// <param name="name"></param>
       /// <param name="value"></param>
       /// <returns></returns>
       public abstract DataPagingDb GetDataPagingDbModel(string name, string value);
       /// <summary>
       /// 保存model到数据库
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public abstract int DataPagingDbAdd(DataPagingDb model);

       /// <summary>
       /// DataSet转化Model
       /// </summary>
       /// <param name="ds"></param>
       /// <returns></returns>
       protected DataPagingDb GetDataPagingDbModel(DataSet ds)
       {
           DataPagingDb model = new DataPagingDb();
           if (ds.Tables[0].Rows.Count > 0)
           {
               DataRow row = ds.Tables[0].Rows[0];
               if (row != null)
               {
                   if (row["Id"] != null && row["Id"].ToString() != "")
                   {
                       model.Id = int.Parse(row["Id"].ToString());
                   }
                   if (row["Status_ClassName"] != null)
                   {
                       model.Status.ClassName = row["Status_ClassName"].ToString();
                   }
                   if (row["DbSelect_TableName"] != null)
                   {
                       model.DbSelect.TableName = row["DbSelect_TableName"].ToString();
                   }
                   if (row["DbSelect_SelectFields"] != null)
                   {
                       model.DbSelect.SelectFields = row["DbSelect_SelectFields"].ToString();
                   }
                   if (row["DbSelect_OrderField"] != null)
                   {
                       model.DbSelect.OrderField = row["DbSelect_OrderField"].ToString();
                   }
                   if (row["DbSelect_OrderType"] != null && row["DbSelect_OrderType"].ToString() != "")
                   {
                       if ((row["DbSelect_OrderType"].ToString() == "1") || (row["DbSelect_OrderType"].ToString().ToLower() == "true"))
                       {
                           model.DbSelect.OrderType = true;
                       }
                       else
                       {
                           model.DbSelect.OrderType = false;
                       }
                   }
                   if (row["DbSelect_PageIndex"] != null && row["DbSelect_PageIndex"].ToString() != "")
                   {
                       model.DbSelect.PageIndex = int.Parse(row["DbSelect_PageIndex"].ToString());
                   }
                   if (row["DbSelect_PageSize"] != null && row["DbSelect_PageSize"].ToString() != "")
                   {
                       model.DbSelect.PageSize = int.Parse(row["DbSelect_PageSize"].ToString());
                   }
                   if (row["DbSelect_StrWhere"] != null)
                   {
                       model.DbSelect.StrWhere = row["DbSelect_StrWhere"].ToString();
                   }
                   if (row["DbSelect_KeyField"] != null)
                   {
                       model.DbSelect.KeyField = row["DbSelect_KeyField"].ToString();
                   }
                   if (row["HtmlTable_TableCssName"] != null)
                   {
                       model.HtmlTable.TableCssName = row["HtmlTable_TableCssName"].ToString();
                   }
                   if (row["HtmlTable_TBodyTrHtml"] != null)
                   {
                       model.HtmlTable.TBodyTrHtml = row["HtmlTable_TBodyTrHtml"].ToString();
                   }
                   if (row["HtmlTable_THeadTrHtml"] != null)
                   {
                       model.HtmlTable.THeadTrHtml = row["HtmlTable_THeadTrHtml"].ToString();
                   }
                   if (row["Operat_OperatHtml"] != null)
                   {
                       model.Operat.OperatHtml = row["Operat_OperatHtml"].ToString();
                   }
                   if (row["Operat_IsHasOperat"] != null && row["Operat_IsHasOperat"].ToString() != "")
                   {
                       if ((row["Operat_IsHasOperat"].ToString() == "1") || (row["Operat_IsHasOperat"].ToString().ToLower() == "true"))
                       {
                           model.Operat.IsHasOperat = true;
                       }
                       else
                       {
                           model.Operat.IsHasOperat = false;
                       }
                   }
                   if (row["Operat_ColumnName"] != null)
                   {
                       model.Operat.ColumnName = row["Operat_ColumnName"].ToString();
                   }
                   if (row["DbOperat_DeleteSql"] != null)
                   {
                       model.DbOperat.DeleteSql = row["DbOperat_DeleteSql"].ToString();
                   }
                   if (row["HtmlFanye_FanyeHtml"] != null)
                   {
                       model.HtmlFanye.FanyeHtml = row["HtmlFanye_FanyeHtml"].ToString();
                   }
                   if (row["HtmlFanye_IsHasFanye"] != null)
                   {
                       if ((row["HtmlFanye_IsHasFanye"].ToString() == "1") || (row["HtmlFanye_IsHasFanye"].ToString().ToLower() == "true"))
                       {
                           model.HtmlFanye.IsHasFanye = true;
                       }
                       else
                       {
                           model.HtmlFanye.IsHasFanye = false;
                       }
                   }
                   //if (row["Where_KeyValue"] != null)
                   //{
                   //    model.Where_KeyValue = row["Where_KeyValue"].ToString();
                   //}

                   if (row["DbOperat_OperatSqlList"] != null)
                   {
                       model.DbOperat.OperatSqlList = row["DbOperat_OperatSqlList"].ToString();
                   }
                   if (row["DbOperat_OrderList"] != null)
                   {
                       model.DbOperat.OrderList = row["DbOperat_OrderList"].ToString();
                   }
                   if (row["DbOperat_SearchList"] != null)
                   {
                       model.DbOperat.SearchList = row["DbOperat_SearchList"].ToString();
                   }
                   if (row["HtmlTable_TableTrCssName"] != null)
                   {
                       model.HtmlTable.TableTrCssName = row["HtmlTable_TableTrCssName"].ToString();
                   }
                   if (row["DbSelect_UniteWhere"] != null)
                   {
                       model.DbSelect.UniteWhere = row["DbSelect_UniteWhere"].ToString();
                   }
                   if (row["Operat_TagString"] != null)
                   {
                       model.Operat.TagString = row["Operat_TagString"].ToString();
                   }
                   if (row["Status_ControlId"] != null)
                   {
                       model.Status.ControlId = row["Status_ControlId"].ToString();
                   }
                   return model;
               }
           }
           else
           {

           }
           return null;
       }

       protected string GetDbTableName()
       {
           return "ARS_DataPaging";
       }

       protected string GetSelectSql()
       {
           //Id,Status_ClassName,DbSelect_TableName,DbSelect_SelectFields,DbSelect_OrderField,DbSelect_OrderType,DbSelect_PageIndex,DbSelect_PageSize,DbSelect_StrWhere,DbSelect_Key,HtmlTable_TableCssName,HtmlTable_TBodyTrHtml,HtmlTable_THeadTrHtml,Operat_OperatHtml,Operat_IsHasOperat,Operat_ColumnName,DbOperat_DeleteSql,Status_UserId,HtmlFanye_FanyeHtml,HtmlFanye_IsHasFanye,Where_KeyValue,DbOperat_OperatSqlList,DbOperat_OrderList,DbOperat_SearchList
           return
               "select  *  from "+this.GetDbTableName()+" where {0}=@{0}";
       }

       /// <summary>
       /// 分页查询的存储过程名称
       /// </summary>
       /// <returns></returns>
       public string GetDataPaging_List_ProcName()
       {
           return "ARS_DataPaging_GetDataList";
       }
       /// <summary>
       /// 添加修改的名称
       /// </summary>
       /// <returns></returns>
       public string GetDataPaging_Model_ProcName()
       {
           return "ARS_DataPaging_Model_ADD";
       }


   }
}
